package com.atguigu.spzx.test;


import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.MinioException;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class FileUploader {
  public static void main(String[] args)
      throws IOException, NoSuchAlgorithmException, InvalidKeyException {
    try {
     //创建MinioClient对象
      MinioClient minioClient =
          MinioClient.builder()
                  //设置服务器的地址
              .endpoint("http://localhost:9000")
              .credentials("minioadmin", "minioadmin")
              .build();


      boolean found =
          minioClient.bucketExists(BucketExistsArgs.builder().bucket("spzx").build());
      if (!found) {

        minioClient.makeBucket(MakeBucketArgs.builder().bucket("spzx").build());
      } else {
        System.out.println("Bucket 'spzx' already exists.");
      }
      //上传文件到minio服务器
      minioClient.uploadObject(
          UploadObjectArgs.builder()
                  //设置bucket名字
              .bucket("spzx")
                  //文件上传到服务器后的名字
              .object("山海经美女.jpg")
                  //指定上传文件的路径
              .filename("D:/feiq/Recv Files/雷锋同学友情提供更新版/2028271.jpg")
              .build());
      System.out.println(
          "'D:/feiq/Recv Files/雷锋同学友情提供更新版/2028271.jpg' is successfully uploaded as "
              + "object '山海经美女.jpg' to bucket 'spzx'.");
    } catch (MinioException e) {
      System.out.println("Error occurred: " + e);
      System.out.println("HTTP trace: " + e.httpTrace());
    }
  }
}